package com.haixee.parkdemo.gateway.client;

import com.haixee.parkdemo.common.entity.AdminLogin;
import com.haixee.parkdemo.common.entity.AuthData;
import com.haixee.parkdemo.common.response.ResResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class AuthService {

    @Autowired
    private RestTemplate restTemplate;

    public ResResult<AuthData> getAuth(String token, Byte method, String router) {

        Map<String, Object> body = new HashMap<>();
        body.put("token", token);
        body.put("method", method);
        body.put("router", router);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        HttpEntity requestEntity = new HttpEntity<>(body, headers);


        ResponseEntity<ResResult<AuthData>> entity = restTemplate.exchange("http://auth/rpc/user/acls", HttpMethod.POST, requestEntity, new ParameterizedTypeReference<ResResult<AuthData>>() {});
        return entity.getBody();
    }

    public ResResult<AdminLogin> login(String username, String password) {

        Map<String, Object> body = new HashMap<>();
        body.put("username", username);
        body.put("password", password);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        HttpEntity requestEntity = new HttpEntity<>(body, headers);


        ResponseEntity<ResResult<AdminLogin>> entity = restTemplate.exchange("http://auth/rpc/login", HttpMethod.POST, requestEntity, new ParameterizedTypeReference<ResResult<AdminLogin>>() {});
        return entity.getBody();
    }
}
